package com.golden.order02.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.golden.order02.dao.OOrderMapper;
import com.golden.order02.domain.OrderDO;
import com.golden.order02.domain.ResultDO;
import com.golden.order02.entity.OOrder;
import com.golden.order02.query.OOrderExample;

@Service
public class OrderServiceImpl implements OrderService{

	@Autowired
	OOrderMapper orderMapper;
	
	@Autowired
	OrderGroupServiceImpl  orderGroupService;
	
	public ResultDO<OrderDO> get( Integer id){
		
		ResultDO<OrderDO> result=new ResultDO<OrderDO>();
		BaseServiceImpl<OrderDO,OOrder> bi=new BaseServiceImpl<>();
		OrderDO oo=new OrderDO();
		
		try {
			OOrder selectByPrimaryKey = orderMapper.selectByPrimaryKey(id);
			OrderDO t = bi.getT(selectByPrimaryKey, oo);
			result.setData(t);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setErrorCode(ResultDO.ORDER_QUERY_ERROR);
			result.setErrorMsg(ResultDO.ORDER_QUERY_MSG_ERROR);
		}
		return result;
	}
	
	public ResultDO<List<OrderDO>> query( OOrderExample example){
		ResultDO<List<OrderDO>> result=new ResultDO<List<OrderDO>>();
		BaseServiceImpl<OrderDO,OOrder> bi=new BaseServiceImpl<>();
		OrderDO oo=new OrderDO();
		try {
			List<OOrder> selectByExample = orderMapper.selectByExample(example);
			List<OrderDO> tList = bi.getTList(selectByExample, oo);
			result.setData(tList);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.ORDER_QUERY_ERROR);
			result.setErrorMsg(ResultDO.ORDER_QUERY_MSG_ERROR);
		}
		return result;
	}
	
	public ResultDO<OrderDO> add( OrderDO beanDO){
		ResultDO<OrderDO> result=new ResultDO<OrderDO>();
		BaseServiceImpl<OOrder, OrderDO> bi=new BaseServiceImpl<>();
		OOrder oi=new OOrder();
		OOrder t = bi.getT(beanDO, oi);
		try {
			System.out.println(beanDO);
			int insert = orderMapper.insert(t);
			if(insert<=0) {
				throw new Exception();
			}
			beanDO.setId(t.getId());
			result.setData(beanDO);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setErrorCode(ResultDO.ORDER_ADD_ERROR);
			result.setErrorMsg(ResultDO.ORDER_ADD_MSG_ERROR);
		}
		return result;
	}
	
	public ResultDO<Integer> delete( Integer id){
		ResultDO<Integer> result=new ResultDO<Integer>();
		try {
			int deleteByPrimaryKey = orderMapper.deleteByPrimaryKey(id);
			if(deleteByPrimaryKey<=0) {
				throw new Exception();
			}
			result.setData(id);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.ORDER_DELETE_ERROR);
			result.setErrorMsg(ResultDO.ORDER_DELETE_MSG_ERROR);
		}
		return result;
	}
	
	public ResultDO<OrderDO> modify( OrderDO beanDO){
		ResultDO<OrderDO> result=new ResultDO<OrderDO>();
		BaseServiceImpl<OOrder, OrderDO> bi=new BaseServiceImpl<>();
		OOrder oi=new OOrder();
		OOrder t = bi.getT(beanDO, oi);
		try {
			int insert = orderMapper.updateByPrimaryKeySelective(t);
			if(insert<=0) {
				throw new Exception();
			}
			result.setData(beanDO);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.ORDER_MODIFY_ERROR);
			result.setErrorMsg(ResultDO.ORDER_MODIFY_MSG_ERROR);
		}
		return result;
	}

	@Override
	public String dddd(Integer id) {
		// TODO Auto-generated method stub
		return orderGroupService.addCont(id);
	}
	

}
